package com.caifu.mapper;

import com.caifu.pojo.YlDayBill;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 日账单表 Mapper 接口
 * </p>
 *
 * @author GZ
 * @since 2020-11-06
 */
public interface YlDayBillMapper extends BaseMapper<YlDayBill> {

    //生成日账单
    @Select("select e.user_no as user_no," +
            " IFNULL(yc.cost_no,0) as 'assess_cost_no',IFNULL(yc.COST_PRICE,0) as 'assess_fee'," +
            " IFNULL(bc.cost_no,0) as 'bed_cost_no',IFNULL(bc.COST_PRICE,0) as 'bed_fee'," +
            " IFNULL(nc.cost_no,0) as 'nurse_cost_no',IFNULL(nc.COST_PRICE,0) as 'nurse_fee'," +
            " IFNULL(fc.cost_no,0) as 'food_cost_no',IFNULL(fc.COST_PRICE,0) as 'food_fee'" +
            " from yl_elderly_info e" +
            " left join yl_assessment_record y on e.user_no=y.USER_NO and y.is_effective='1'" +
            " left join yl_cost yc on y.cost_no=yc.COST_NO and yc.`STATUS`='1'" +
            " left join yl_bed_record b on e.user_no=b.USER_NO and b.is_effective='1'" +
            " left join yl_cost bc on b.BED_FEE_TYPE =bc.COST_NO and bc.`STATUS`='1'" +
            " left join yl_nursing_record n on e.user_no=n.USER_NO and n.is_effective='1'" +
            " left join yl_cost nc on n.cost_no =nc.COST_NO and nc.`STATUS`='1'" +
            " left join yl_cost fc on e.PENSION_NO =fc.PENSION_NO and fc.COST_TYPE='104'" +
            " where e.USER_NO=#{userNo}")
    Map<String,Object> createDayBill(@Param("userNo") String userNo,@Param("day") String day);

    //获取养老院老人 信息列表
    List<Map<String, Object>> getElderlyInfoPageList(Map<String, Object> params);

    //获取养老院老人 信息列表 总数
    Integer getElderlyInfoPageListTotalCount(Map<String, Object> params);

    //获取用户初始的月结算金额
    @Select("select case when y.USER_NO is not null then 1 else 0 end as billExist, " +
            "IFNULL(sum(y.BED_FEE),0) as bedFee, " +
            "IFNULL(sum(y.NURSING_FEE),0) as nursingFee, " +
            "IFNULL(sum(y.ASSEMENT_FEE),0) as assementFee, " +
            "IFNULL(sum(y.MEAL_FEE),0) as mealFee " +
//            "case when IFNULL(sum(y.BED_FEE),0)=0 then 0 else round(sum(y.BED_FEE)/count(*),2) end as dayBedFee, " +
//            "case when IFNULL(sum(y.NURSING_FEE),0)=0 then 0 else round(sum(y.NURSING_FEE)/count(*),2) end as dayNursingFee, " +
//            "case when IFNULL(sum(y.ASSEMENT_FEE),0)=0 then 0 else round(sum(y.ASSEMENT_FEE)/count(*),2) end as dayAssementFee, " +
//            "case when IFNULL(sum(y.MEAL_FEE),0)=0 then 0 else round(sum(y.MEAL_FEE)/count(*),2) end as dayMealFee " +
            "from yl_day_bill y where y.USER_NO=#{userNo} and SUBSTR(y.BILL_DATE,1,4)=#{year} and SUBSTR(y.BILL_DATE,5,2)=#{month}")
    Map<String, Object> getUserInitMonthBill(@Param("userNo")String userNo, @Param("year")int year, @Param("month")int month);

}
